package general.socketmanager;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class cClientThread implements Runnable {
	
	private int iID;
	private Socket oSocket;
	private iClientThreadListener oListener;
	private ObjectInputStream oOIS;
	private ObjectOutputStream oOOS;
	
	public cClientThread(int iID, Socket oSocket, iClientThreadListener oListener, ObjectInputStream oOIS, ObjectOutputStream oOOS) {
		this.iID = iID;
		this.oSocket = oSocket;
		this.oListener = oListener;
		this.oOIS = oOIS;
		this.oOOS = oOOS;
	}
	
	public void SetListener(iClientThreadListener oListener) {
		this.oListener = oListener;
	}

	@Override
	public void run() {
		try {
			Object oReadObject;

			while (true) {
				oReadObject = oOIS.readObject();  

				if (oReadObject != null) {
					oListener.OnObjectReceived(iID, oReadObject);
				}  
			}
		} catch (IOException | ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			// disconnect
			oListener.OnConnectionClosed(iID);
			return;
		}
	}
	
	/*
	public void run() {
		int iTemp;
		byte[] bMessage;
		try {
			DataInputStream dIn = new DataInputStream(oSocket.getInputStream());

			while (true) {
				iTemp = dIn.readInt();                // read length of incoming message
				if (iTemp>0) {
					bMessage = new byte[iTemp];
				    dIn.readFully(bMessage, 0, bMessage.length); // read the message
					oListener.OnDataReceived(iID, bMessage);
				}

			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			// disconnect
			
		}
	}
	*/

}
